package com.ebs.md.fxmonitor.client.impl;

import java.util.List;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import com.ebs.md.fxmonitor.client.api.DealCodeSubscription;

/**
 * <p>Title: Netlink Project</p>
 *
 * <p>Description: Netlink Component</p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: ICAP</p> $Id: //depot/MarketData/netlink/main/src/main/java/com/ebs/md/netlink/client/impl/GenericDealCodeSubscription.java#7 $ not attributable $Revision: #7 $ 1.0
 */
public class GenericDealCodeSubscription implements DealCodeSubscription{
    private List<String> m_currenciesList = new ArrayList<String>();
    private Set<String> m_currencies = new HashSet<String>();
    private String m_dealCode;
    private int m_floorKey;
    private String m_csv;


    public GenericDealCodeSubscription(String dealCode, int floorKey, List<String> currencies)
    {
        m_dealCode = dealCode;
        m_floorKey = floorKey;
        setCurrencies(currencies);
    }

    public GenericDealCodeSubscription()
    {
        this(null, 0, new ArrayList<String>());
    }

    /**
     * getCurrenciesList
     *
     * @return List of currencies
     */
    public List<String> getCurrenciesList() {
        return m_currenciesList;
    }

    /**
     * getCurrenciesList
     *
     * @return Set of currencies
     */
    public Set<String> getCurrencies() {
        return m_currencies;
    }

    /**
     * getDealCode
     *
     * @return String
     */
    public String getDealCode() {
        return m_dealCode;
    }

    /**
     * getFloorKey
     *
     * @return int
     */
    public int getFloorKey() {
        return m_floorKey;
    }


    public void setDealCode(String dc){
        m_dealCode = dc;
    }

    public void setFloorKey(int fk){
        m_floorKey = fk;
    }

    public void addCurrency(String c){
        m_currencies.add(c);
        m_currenciesList.add(c);
        m_csv = null;
    }

    public synchronized void setCurrencies(List<String> c)
    {
        m_currenciesList = c;
        m_currencies.addAll(c);
        m_csv = null;
    }

    public synchronized String getCurrenciesCsv(){
        if(null == m_csv){
            StringBuilder builder = new StringBuilder(256);
            boolean theFirst = true;
            Set<String> dups = new HashSet<String>();
            for (String ccy : m_currenciesList) {
                if(dups.contains(ccy)){
                    continue;
                }else{
                    dups.add(ccy);
                }
                if (!theFirst) {
                    builder.append(",");
                }
                builder.append(ccy);
                theFirst = false;
            }
            m_csv = builder.toString();
        }
        return m_csv;
    }

    public String toString() {
        return "Deal Code: " + m_dealCode + "; Floor Key: " + m_floorKey + "; Currencies: " + getCurrenciesCsv();
    }

    public int hashCode() {
        return m_floorKey;
    }

    public boolean equals(Object obj) {
        if(obj instanceof DealCodeSubscription){
            DealCodeSubscription other = (DealCodeSubscription)obj;
            return m_floorKey == other.getFloorKey() && m_dealCode.equals(other.getDealCode());
        }
        return false;
    }

}
